﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;

using MissedMemo.MeatAndCheeseDisc.OrderEntry.OrderProcessing;


namespace MissedMemo.MeatAndCheeseDisc.OrderEntry
{
    public partial class viewOrderDetails : UserControl
    {
        public viewOrderDetails()
        {
            InitializeComponent();

            if( false == DesignerProperties.GetIsInDesignMode( this ) )
            {
                foreach( string strPizzaType in LookupMenuItem.GetTypes() )
                    comboPizzaType.Items.Add( strPizzaType );

                foreach( string strPizzaSize in LookupMenuItem.GetSizes() )
                    comboPizzaSize.Items.Add( strPizzaSize );

                comboPizzaType.SelectedIndex = 0;
                comboPizzaSize.SelectedIndex = 0;

                DataContextChanged += OnDataContextChanged;
            }
        }


        void OnDataContextChanged( object sender, DependencyPropertyChangedEventArgs e )
        {
            // Set embedded WinForms control value (can't bind)
            Order order = e.NewValue as Order;
            if( order != null )
            {
                datepickerOrderExpected.Value = order.DateTimeOrderExpectedComplete;
            }
        }


        private void btnAddItem_Click( object sender, System.Windows.RoutedEventArgs e )
        {
            Order order = DataContext as Order;

            if( order != null )
            {
                Pizza pizza = new Pizza();

                pizza.Type = comboPizzaType.SelectedValue.ToString();
                pizza.Size = comboPizzaSize.SelectedValue.ToString();
                pizza.UpdatePrice();

                order.Items.Add( pizza );
            }
        }


        private void btnX_Click( object sender, System.Windows.RoutedEventArgs e )
        {
            Order order = DataContext as Order;

            if( order != null )
            {
                Pizza pizza = ( sender as FrameworkElement ).DataContext as Pizza;

                if( pizza != null )
                {
                    order.Items.Remove( pizza );
                }
            }
        }


        // Expected completion DateTime (or scheduled FUTURE delivery)

        private void OnEditDateTimeOrderExpected( object sender, System.EventArgs e )
        {
            Order order = DataContext as Order;
            if( order != null )
            {
                order.DateTimeOrderExpectedComplete = datepickerOrderExpected.Value;
            }
        }
    }
}
